package im.starchat.chat.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import im.starchat.chat.R;
import im.starchat.chat.activity.StarActivity;
import im.starchat.chat.impl.IFollowingCallback;
import im.starchat.chat.rest.model.ChannelBean;
import im.starchat.chat.utils.PreferencesStorageUtil;
import im.starchat.chat.view.CircularImageButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 关注界面适配器
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private static final int TYPE_ITEM = 1001;
    private static final int TYPE_EMPTY = 1002;

    private List<ChannelBean> mList;
    private IFollowingCallback mIFollowingCallback;

    public RecommendAdapter(Context context, IFollowingCallback iFollowingCallback) {
        this.mContext = context;
        this.mIFollowingCallback = iFollowingCallback;
    }


    public void refreshData(List<ChannelBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void addAll(List<ChannelBean> list){
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    public void changeAttentionState(int position, boolean state){
        mList.get(position).setIs_following(state);
        notifyItemChanged(position);
    }

    public void changeBatchAttentionState( boolean state){
        if(mList != null){
            for(ChannelBean cb : mList){
                cb.setIs_following(state);
            }
        }
        notifyDataSetChanged();
    }


    public int[] getFollowedIdint(){
        List<ChannelBean> channelBeans = new ArrayList<>();
        if(mList != null && mList.size() > 0){
            for(ChannelBean cb : mList){
                if(cb.is_following()){
                    channelBeans.add(cb);
                }
            }
        }
        if(channelBeans.size() > 0){
            int[] ids = new int[channelBeans.size()];
            for(int num = 0; num < channelBeans.size() ; num ++){
                ids[num] = channelBeans.get(num).getId();
            }
            return ids;
        }

        return null;
    }

//    public String getFollowedIdString(){
//        StringBuilder sb = new StringBuilder();
//        sb.append("[");
//
//        if(mList != null && mList.size() > 0){
//            for(ChannelBean cb : mList){
//                if(cb.is_following()){
//                    sb.append(cb.getId() + ",");
//                }
//            }
//            if(sb.length() > 1){
//                sb.deleteCharAt(sb.length()-1);
//            }
//
//        }
//        sb.append("]");
//
//        return sb.toString();
//    }
//
//    public String getUnFollowedIdString(){
//        StringBuilder sb = new StringBuilder();
//        sb.append("[");
//
//        if(mList != null && mList.size() > 0){
//            for(ChannelBean cb : mList){
//                if(!cb.is_following()){
//                    sb.append(cb.getId() + ",");
//                }
//            }
//            if(sb.length() > 1){
//                sb.deleteCharAt(sb.length()-1);
//            }
//        }
//        sb.append("]");
//
//        return sb.toString();
//    }


    public int[] getUnFollowedIdInt(){
        List<ChannelBean> channelBeans = new ArrayList<>();
        if(mList != null && mList.size() > 0){
            for(ChannelBean cb : mList){
                if(!cb.is_following()){
                    channelBeans.add(cb);
                }
            }
        }
        if(channelBeans.size() > 0){
            int[] ids = new int[channelBeans.size()];
            for(int num = 0; num < channelBeans.size() ; num ++){
                ids[num] = channelBeans.get(num).getId();
            }
            return ids;
        }

        return null;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ITEM:
                return new FanViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_contribution, parent, false));
            case TYPE_EMPTY:
            default:
                return new EmptyHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.view_empty, parent, false));

        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_ITEM:
                FanViewHolder fanViewHolder = (FanViewHolder) holder;

                fanViewHolder.mName.setText(mList.get(position).getNickname());
                fanViewHolder.mDescribe.setText(mList.get(position).getDescription());

                if (!TextUtils.isEmpty(mList.get(position).getAvatar())) {
                    Picasso.with(mContext).load(mList.get(position).getAvatar())
                            .placeholder(R.drawable.home_bg)
                            .into(fanViewHolder.imageButtonUserLogo);
                }
                fanViewHolder.imageButtonUserLogo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!PreferencesStorageUtil.getUserId(mContext).equals(mList.get(position).getId())) {
                            Intent viewUserInfointent = new Intent();
                            viewUserInfointent.setClass(mContext, StarActivity.class);
                            viewUserInfointent.putExtra(StarActivity.USER_ID, mList.get(position).getId());
                            mContext.startActivity(viewUserInfointent);
                        }
                    }
                });

                if(mList.get(position).is_following()){
                    fanViewHolder.mCancelAttentionBtn.setVisibility(View.VISIBLE);
                    fanViewHolder.mAttentionBtn.setVisibility(View.GONE);
                }else{
                    fanViewHolder.mCancelAttentionBtn.setVisibility(View.GONE);
                    fanViewHolder.mAttentionBtn.setVisibility(View.VISIBLE);
                }
                fanViewHolder.mAttentionBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mIFollowingCallback != null){
                            mIFollowingCallback.onFollowing(mList.get(position).getId() + "", position);
                        }
                    }
                });
                fanViewHolder.mCancelAttentionBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mIFollowingCallback != null){
                            mIFollowingCallback.onCancelFollowing(mList.get(position).getId() + "", position);
                        }
                    }
                });

//                emptyHolder.mInfoText.setText("无新消息");
                break;
            case TYPE_EMPTY:
                EmptyHolder emptyHolder = (EmptyHolder) holder;
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (mList == null || mList.size() == 0) {
            return TYPE_EMPTY;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 1;
        }
        return mList.size();
    }

    public class FanViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.avator)
        CircularImageButton imageButtonUserLogo;
        @InjectView(R.id.attention)
        TextView mAttentionBtn;
        @InjectView(R.id.cancel_attention)
        TextView mCancelAttentionBtn;
        @InjectView(R.id.name)
        TextView mName;
        @InjectView(R.id.describe)
        TextView mDescribe;

        public FanViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }


    public class EmptyHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.info_text)
        TextView mInfoText;

        public EmptyHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
